package com.terra;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager;
import android.util.Log;

import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class Globals extends terraDroid {
	
	static Context myContext;
	static double dGPSLat = 0;
	static double dGPSLon = 0;
	static double dHeading = 0;
	static double dPitch;
	static double dScaleHeading = 0.8;
	static double dScalePitch = 0.8;
	static Boolean bTrackGPS = false;
	static String strLastFile = "";
	static String strLocalHTMLPath = "";
	static String strTempCachesPath = "";	
	//static int nUnits = 0;
	static String strUnits = "English";
    static Boolean bLoadLastFile = false;
    static 	double dTargetLat;
	static double dTargetLon;
	//public static MyCacheHandler tempCachesList;
	static String strUsername = "";
	static String strPassword = "";
	
	static Drawable drawablePin = null;
	static Drawable drawableRedPin = null;
	
	static MapItemizedOverlay TCOverlay = null;
	static MapItemizedOverlay TempOverlay = null;
	static MyLocationOverlay myLocationOverlay = null;
	static MapView mapView;
	
    static public void OnResume() {

    	SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(myContext);
    	strUnits = prefs.getString("unitPref", "English");
    	bLoadLastFile = prefs.getBoolean("LoadLastFile", false);
    	strLocalHTMLPath = prefs.getString("localHTML", "/sdcard");
    	strUsername = prefs.getString("Username", "");
    	strPassword = prefs.getString("Password", "");
    	strLastFile = prefs.getString("LastFile", "");
    	
        Log.d("Debug", "Resume Globals");
        Log.d("Debug", "Units 1 "+Globals.strUnits+", Last File "+Globals.bLoadLastFile+", Path "+Globals.strLocalHTMLPath+", User "+Globals.strUsername+", Pw "+Globals.strPassword);
    }
    
    static public void OnPause() {
    	
    	Log.d("Debug", "Resetting reset on next run");
    	SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(myContext);
    	SharedPreferences.Editor editor = prefs.edit();
    	editor.putBoolean("Reset", false);
    	editor.commit();
    	
        Log.d("Debug", "Globals on pause");
    }
    
    static public void SaveFileNow(String file) {
    	SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(myContext);
        SharedPreferences.Editor editor = prefs.edit();
    	editor.putString("LastFile", file);
    	editor.commit();
    	Globals.strLastFile = file;
    	
    }
    
    static public void Reset() {

    	SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(myContext);
    	SharedPreferences.Editor editor = prefs.edit();
    	editor.putBoolean("Reset", true);
    	editor.commit();    
    }
    
    static public Boolean GetReset() {
    	SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(myContext);
    	return prefs.getBoolean("Reset", true);
    }
    
	static public void WriteGPX(String fileName) {
		
		if( fileName=="" || Globals.TempOverlay==null )
			return;
		
        FileWriter filewriter;
		try {
			filewriter = new FileWriter(fileName);
	        BufferedWriter out = new BufferedWriter(filewriter);

            out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            out.write("<gpx>\n");
            out.write("  <metadata>\n    <desc>terraDroid Waypoint File</desc>\n");
            out.write("    <author>Shaggy</author>\n");
            out.write("  </metadata>\n");
            
            for(int i=0; i<Globals.TempOverlay.size(); i++) {
            	TCOverlayItem TCOverlay = (TCOverlayItem) Globals.TempOverlay.getItem(i);
            	Double lat  = TCOverlay.getLatitude();
            	Double lon  = TCOverlay.getLongitude();
            	String name = TCOverlay.getName();
            	String desc = TCOverlay.getDesc();
            	out.write("    <wpt lat=\""+lat+"\" lon=\""+lon+"\">\n");
            	out.write("      <name>"+name+"</name>\n");
            	out.write("      <desc>"+desc+"</desc>\n");
            	out.write("      <sym>terraDroid</sym>\n");
            	out.write("    </wpt>\n");
            }
            
            out.write("</gpx>\n");
            out.close();
		} catch (IOException e) {
			Log.d("Debug", "Error opening file to write "+fileName+":"+e.getMessage());
			e.printStackTrace();
		}
	}
}
